package org.fly.utils;

import org.apache.commons.lang.StringUtils;
import org.fly.bean.BaseUser;
import org.fly.bean.User;
import org.fly.contants.WebConstant;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * @author fly
 */
public class CookieUtil {
public static void setLoginCookie(HttpServletResponse response, BaseUser user, String domain) {
    String token = user.getCookieMap().get(WebConstant.cookie_token);
    Cookie cookie1 = new Cookie(WebConstant.cookie_token, token);
    Cookie cookie2 = new Cookie(WebConstant.cookie_name, user.getCookieMap().get(WebConstant.cookie_name));
    Cookie cookie3 = new Cookie(WebConstant.cookie_fake, user.getCookieMap().get(WebConstant.cookie_fake));
    response.addCookie(setDomainPathExpire(cookie1, domain));
    response.addCookie(setDomainPathExpire(cookie2, domain));
    response.addCookie(setDomainPathExpire(cookie3, domain));
}

    private static Cookie setDomainPathExpire(Cookie cookie, String domain) {
        if (StringUtils.isNotBlank(domain)) {
            cookie.setDomain(domain);
        }
        cookie.setPath("/");
        return cookie;
    }
}